package cn.edu.imau.zy.system.service.impl;

import cn.edu.imau.zy.common.core.entity.system.SysSignDetail;
import cn.edu.imau.zy.common.core.entity.system.SysSignJobDetailVo;
import cn.edu.imau.zy.common.core.entity.system.SysUser;
import cn.edu.imau.zy.common.core.text.Convert;
import cn.edu.imau.zy.common.utils.DateUtils;
import cn.edu.imau.zy.system.mapper.SysSignDetailMapper;
import cn.edu.imau.zy.system.service.ISysSignDetailService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 任务详情Service业务层处理
 * 
 * @author wyz
 * @date 2020-07-07
 */
@Service
public class SysSignDetailServiceImpl extends ServiceImpl<SysSignDetailMapper,SysSignDetail> implements ISysSignDetailService
{
    @Autowired
    private SysSignDetailMapper sysSignDetailMapper;

    /**
     * 查询任务详情
     * 
     * @param id 任务详情ID
     * @return 任务详情
     */
    @Override
    public SysSignDetail selectSysSignDetailById(String id)
    {
        return sysSignDetailMapper.selectSysSignDetailById(id);
    }

    /**
     * 查询任务详情列表
     * 
     * @param sysSignDetail 任务详情
     * @return 任务详情
     */
    @Override
    public List<SysSignDetail> selectSysSignDetailList(Map sysSignDetail)
    {
        return sysSignDetailMapper.selectSysSignDetailList(sysSignDetail);
    }

    /**
     * 新增任务详情
     * 
     * @param sysSignDetail 任务详情
     * @return 结果
     */
    @Override
    public int insertSysSignDetail(SysSignDetail sysSignDetail)
    {
        sysSignDetail.setCreateTime(DateUtils.getNowDate());
        return sysSignDetailMapper.insertSysSignDetail(sysSignDetail);
    }

    /**
     * 修改任务详情
     * 
     * @param sysSignDetail 任务详情
     * @return 结果
     */
    @Override
    public int updateSysSignDetail(SysSignDetail sysSignDetail)
    {
        sysSignDetail.setUpdateTime(DateUtils.getNowDate());
        return sysSignDetailMapper.updateSysSignDetail(sysSignDetail);
    }

    /**
     * 删除任务详情对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteSysSignDetailByIds(String ids)
    {
        return sysSignDetailMapper.deleteSysSignDetailByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除任务详情信息
     * 
     * @param id 任务详情ID
     * @return 结果
     */
    @Override
    public int deleteSysSignDetailById(String id)
    {
        return sysSignDetailMapper.deleteSysSignDetailById(id);
    }

    @Override
    public int insertSysSignDetailList(List<SysSignDetail> insList) {
        return sysSignDetailMapper.insertSysSignDetailList(insList);
    }

    @Override
    public List<SysSignJobDetailVo> selectMySignList(SysUser user) {
        return sysSignDetailMapper.selectMySignList(user);
    }

    @Override
    public SysSignJobDetailVo selectMySign(String id) {
        return sysSignDetailMapper.selectMySign(id);
    }

    @Override
    public Boolean finish(SysSignJobDetailVo sign, SysSignDetail signDetail) {
        Boolean res = false;
        List<Map> list = JSON.parseArray(sign.getMap()).toJavaList(Map.class);

        double maxLat=-200D,maxLng=-200D,minLat=200D,minLng=200D;

        for (Map j:list) {
            maxLat = Double.max(maxLat,Double.valueOf(j.get("latitude").toString()));
            maxLng = Double.max(maxLng,Double.valueOf(j.get("longitude").toString()));
            minLat = Double.min(minLat,Double.valueOf(j.get("latitude").toString()));
            minLng = Double.min(minLng,Double.valueOf(j.get("longitude").toString()));
        }

        if(signDetail.getLatitude().doubleValue()<minLat || signDetail.getLatitude().doubleValue()>maxLat ||signDetail.getLongitude().doubleValue()<minLng||signDetail.getLongitude().doubleValue()>maxLng){
            return res;
        }
        double testLat = signDetail.getLatitude().doubleValue();
        double testLng = signDetail.getLongitude().doubleValue();
        for (int i = 0,j=list.size()-1; i < list.size(); j = i++) {
            double piLat=Double.parseDouble(list.get(i).get("latitude").toString());
            double piLng=Double.parseDouble(list.get(i).get("longitude").toString());
            double pjLat=Double.parseDouble(list.get(j).get("latitude").toString());
            double pjLng=Double.parseDouble(list.get(j).get("longitude").toString());

            if(((piLat>testLat) != (pjLat>testLat))&&
                    (testLng < (pjLng-piLng) * (testLat-piLat) / (pjLat-piLat) + piLng)){
                res = !res;
            }
        }

        if(res){
            signDetail.setSignTime(LocalDateTime.now());
            signDetail.setStatus(2L);
            sysSignDetailMapper.updateSysSignDetail(signDetail);
        }
        return res;
    }

    @Override
    public void deleteSysSignDetailByJobIds(String ids) {
        sysSignDetailMapper.deleteSysSignDetailByJobIds(Convert.toStrArray(ids));
    }

    @Override
    public int deleteSysSignDetail(Map<String, Object> params) {
        return sysSignDetailMapper.deleteSysSignDetail(params);
    }
}
